/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.webservice.jaxb.util;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Magri
 */
public class XmlUtil {

    String schemaName = "Schema.xsd";
    public String objetoParaXml(Object obj) {
        try {

            JAXBContext contexto = JAXBContext.newInstance(obj.getClass());

            Marshaller marchaller = contexto.createMarshaller();

            marchaller.setSchema(getXSD(schemaName));
            marchaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marchaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            StringWriter resultado = new StringWriter();

            marchaller.marshal(obj, resultado);

            return resultado.toString();

        } catch (JAXBException ex) {
            Logger.getLogger(XmlUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public Object xmlParaObjeto(Reader reader, Class pClass) {
        try {

            JAXBContext contexto = JAXBContext.newInstance(pClass.getClass());

            Unmarshaller unmarshaller = contexto.createUnmarshaller();

            unmarshaller.setSchema(getXSD(schemaName));
            //unmarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            //marchaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            return unmarshaller.unmarshal(reader);

        } catch (JAXBException ex) {
            Logger.getLogger(XmlUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public Schema getXSD(String nomeArquivo) {
        try {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            
            Schema schema = factory.newSchema();
            
            return schema;
        } catch (SAXException ex) {
            Logger.getLogger(XmlUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public void gerarXSD(Class pClass) {
        try {
            JAXBContext contexto = JAXBContext.newInstance(pClass);

            contexto.generateSchema(new MySchemaOutputResolver(schemaName));

        } catch (IOException | JAXBException ex) {
            Logger.getLogger(XmlUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    class MySchemaOutputResolver extends SchemaOutputResolver {

        private String arquivoSaida;

        public MySchemaOutputResolver(String arquivoSaida) {
            this.arquivoSaida = arquivoSaida;
        }

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            System.out.println("createOutput: namespaceUri: " + namespaceUri + " suggestedFileName: " + suggestedFileName);
            StreamResult result = new StreamResult(new File(arquivoSaida));

            return result;
        }
    }
}
